package bm.com.project.server.project.service.impl;

import bm.com.framework.security.util.SecurityUtils;
import bm.com.project.server.common.dto.SearchBaseDTO;
import bm.com.project.server.project.dto.BmProjectMilestoneDTO;
import bm.com.project.server.project.entity.BmProjectEntity;
import bm.com.project.server.project.mapper.BmProjectMapper;
import bm.com.project.server.project.service.BmProjectMilestoneService;
import bm.com.project.server.project.service.BmProjectService;
import bm.com.framework.common.resp.PageVO;
import bm.com.framework.common.resp.ResultVO;
import bm.com.framework.common.unit.DateUnit;
import bm.com.framework.common.unit.Tool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("bmProjectService")
public class BmProjectServiceImpl extends ServiceImpl<BmProjectMapper, BmProjectEntity> implements BmProjectService {

    @Resource
    BmProjectMapper bmProjectDao;

    @Resource
    BmProjectMilestoneService bmProjectMilestoneService;

    @Override
    public ResultVO saveProject(BmProjectEntity bmProject) {

        /**
         * 添加项目信息
         */
        if(Tool.isStringEmpty(bmProject.getId())){
            String projectId = Tool.iDGenerator();
            bmProject.setId(projectId);

            /**
             * 构建里程碑信息
             */
            BmProjectMilestoneDTO bmProjectMilestoneDTO = new BmProjectMilestoneDTO();
            bmProjectMilestoneDTO.setProjectId(projectId);
//            bmProjectMilestoneDTO.set(DateUnit.getNowTimeSecond());
            bmProjectMilestoneDTO.setTitle("项目建立");
            bmProjectMilestoneDTO.setMark("创建项目");
            bmProjectMilestoneService.create(bmProjectMilestoneDTO);
        }
        bmProject.setCreator(SecurityUtils.getLoginUserId());
        bmProject.setOrgId(SecurityUtils.getOrgId());
        bmProject.setCreateDate(DateUnit.getNowTimeSecond());
        saveOrUpdate(bmProject);


        return ResultVO.success("创建成功");

    }

    /**
     * 获取项目列表
     * @param queue
     * @return
     */
    @Override
    public ResultVO getList(SearchBaseDTO queue) {

        LambdaQueryWrapper<BmProjectEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmProjectEntity::getOrgId, SecurityUtils.getOrgId());
        if (Tool.isStringEmpty(queue.getKeyword())) {
            wrapper.like(BmProjectEntity::getTitle, queue.getKeyword());
        }

        queue.setPageOffset((queue.getPage() - 1) * queue.getPageNum());
        queue.setOrgId(SecurityUtils.getOrgId());
        Long l = bmProjectDao.selectCount(wrapper);
        List<BmProjectEntity> list = bmProjectDao.getList(queue);

        PageVO<Object> PageVO = new PageVO<>(2, list);
        return ResultVO.success(PageVO);
    }

    /**
     * 查看详情
     * @param projectId
     * @return
     */
    @Override
    public ResultVO getDetail(String projectId) {
       return ResultVO.success(bmProjectDao.detail(projectId));
    }


}